from app import db
from flask_security import RoleMixin, UserMixin
from werkzeug.security import generate_password_hash, check_password_hash  # 生成密码散列值函数


# 创建数据模型对象
class Department(db.Model):  # 部门
    departmentID = db.Column(db.Integer, primary_key=True, autoincrement=True)
    departmentname = db.Column(db.String(100), nullable=False, unique=True)
    departmenttype = db.Column(db.Enum('部门', '公司'), server_default='部门', nullable=False)
    departmentphone = db.Column(db.BigInteger, nullable=False, unique=True)
    departmentfax = db.Column(db.BigInteger, nullable=True)
    describe = db.Column(db.Text, nullable=True)
    superior = db.Column(db.String(100), nullable=True)
    establish = db.Column(db.Date, nullable=False)


class Post(db.Model):  # 岗位
    postID = db.Column(db.Integer, primary_key=True, autoincrement=True)
    postname = db.Column(db.String(100), nullable=False, unique=True)
    posttype = db.Column(db.Enum('市场', '营销', '技术', '管理'), server_default='技术', nullable=False)
    establishment = db.Column(db.Integer, nullable=True)


class Staff(db.Model):  # 职员信息
    userID = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), nullable=False)
    sex = db.Column(db.Enum('男', '女'), server_default='男', nullable=False)
    birthday = db.Column(db.Date, nullable=False)
    IDnumber = db.Column(db.String(18), nullable=False, unique=True)
    departmentID = db.Column(db.Integer, db.ForeignKey('department.departmentID'))  # 添加外键
    postID = db.Column(db.Integer, db.ForeignKey('post.postID'))
    hiredate = db.Column(db.Date, nullable=False)
    takejobdate = db.Column(db.Date, nullable=False)
    employment_form = db.Column(db.Enum('正式员工', '临时员工'), server_default='正式员工', nullable=False)
    personnel_source = db.Column(db.Enum('校园招聘', '社会招聘', '其他'), server_default='社会招聘', nullable=False)
    politics_status = db.Column(db.Enum('党员', '预备党员', '团员', '其他'))
    nation = db.Column(db.Enum('基诺族', '珞巴族', '门巴族', '赫哲族', '鄂伦春族', '独龙族', '塔塔尔族', '京族',
                               '裕固族', '保安族', '德昂族', '鄂温克族', '俄罗斯族', '乌孜别克族', '怒族',
                               '塔吉克族', '普米族', '阿昌族', '锡伯族', '仡佬族', '毛南族', '撒拉族',
                               '布朗族', '羌族', '仫佬族', '达斡尔族', '土族', '柯尔克孜族', '景颇族',
                               '纳西族', '东乡族', '水族', '拉祜族', '高山族', '畲族', '佤族', '僳僳族',
                               '黎族', '傣族', '哈萨克族', '哈尼族', '土家族', '白族', '瑶族', '侗族',
                               '满族', '朝鲜族', '布依族', '壮族', '彝族', '苗族', '维吾尔族', '藏族',
                               '回族', '蒙古族', '汉族'))
    native_place = db.Column(db.Text)
    phone = db.Column(db.BigInteger, nullable=False, unique=True)
    email = db.Column(db.String(50), nullable=False, unique=True)
    height = db.Column(db.Integer)
    bloodtype = db.Column(db.Enum('A型', 'B型', 'AB型', 'O型', '其他'))
    marriage = db.Column(db.Enum('未婚', '已婚', '丧偶', '离婚', '其他'))
    birthplace = db.Column(db.Text)
    domicileplace = db.Column(db.Text)
    highest_degree = db.Column(db.Enum('高中及以下', '大专', '本科', '研究生'))
    highest_offering = db.Column(db.Enum('无学位', '学士', '双学士', '硕士', '博士', '博士后'))
    graduate_school = db.Column(db.String(255))
    major = db.Column(db.String(255))
    graduation_date = db.Column(db.Date)
    now = db.Column(db.Boolean)


roles_users = db.Table('roles_users',
                       db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
                       db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))


class Role(db.Model, RoleMixin):  # 权限
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))


class User(db.Model, UserMixin):  # 用户
    id = db.Column(db.Integer, primary_key=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    password = db.Column(db.String(128), nullable=False)
    phone = db.Column(db.BigInteger, nullable=False, unique=True)
    email = db.Column(db.String(50), nullable=False, unique=True)
    active = db.Column(db.Boolean())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    def set_password(password):  # 用来设置密码的方法，接受密码作为参数
        return generate_password_hash(password)  # 将生产的密码保持到对应的字段

    def validate_password(self, password):  # 用于验证密码的方法，接受密码作为参数
        return check_password_hash(self.password, password)
        # 返回布尔值


class Sign_in(db.Model):  # 打卡
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    late_date = db.Column(db.Date, nullable=False, server_default='1999-01-01')
    late_time = db.Column(db.Float, nullable=False, server_default='-1')


class Overtime(db.Model):  # 加班
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    overtime_date = db.Column(db.Date, nullable=False, server_default='1999-01-01')
    overtime_hour = db.Column(db.Float, nullable=False, server_default='-1')


class Holiday(db.Model):  # 休假
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    cause = db.Column(db.Text, nullable=False)
    state = db.Column(db.Enum('审核中', '审核不通过', '审核通过'), server_default='审核中', nullable=False)
    starting_time = db.Column(db.Date, nullable=False)
    ending_time = db.Column(db.Date, nullable=False)
    no_pass_cause = db.Column(db.Text)


class Evection(db.Model):  # 出差
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userID = db.Column(db.Integer, db.ForeignKey('staff.userID'), nullable=False)
    cause = db.Column(db.Text, nullable=False)
    state = db.Column(db.Enum('审核中', '审核不通过', '审核通过'), server_default='审核中', nullable=False)
    starting_time = db.Column(db.Date, nullable=False)
    ending_time = db.Column(db.Date, nullable=False)
    no_pass_cause = db.Column(db.Text)


class Nation(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    nation = db.Column(db.String(20), nullable=False)

# db.create_all()
